import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_pu_user_locator import AdminPUUserLocator
from Case.python_ui.Common.commontool import CommonTool
from selenium.common.exceptions import StaleElementReferenceException
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.PageLocate.admin_pu_usergroup_locator import AdminPUUserGroupLocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.organization_data import Organization
from Case.python_ui.Test_data.user_data import User


class  AdminPUUserPages(Basepage):

     def  enter_the_user_page(self):
          doc='进入admin端的user首页'
          time.sleep(5)
          for i in range(3):
               try:
                    self.wait_elevisble(AdminPUUserLocator.paltform_user_tab,doc)
                    self.move_mouse_to_click_element(AdminPUUserLocator.paltform_user_tab,doc)
                    self.wait_elevisble(AdminPUUserLocator.user_tab,doc)
                    self.move_mouse_to_click_element(AdminPUUserLocator.user_tab,doc)
                    break
               except:
                    self.refresh_page()
                    time.sleep(5)
                    continue
          self.wait_elevisble(AdminPUUserLocator.page_total,doc)

     # Platform User->User页面用户具体信息overview tab页
     def platform_user_member_information_overview_tab(self):
          time.sleep(2)
          doc = "Platform User->User页面用户具体信息overview tab页"
          self.move_mouse_to_click_element(AdminPUUserLocator.platform_user_account_email, doc)
          time.sleep(1)
          self.wait_elevisble(AdminPUUserLocator.platform_user_member_specific_info_page, doc)
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_basic_info_overview_title, doc).text, 'Basic Information')

      # Platform User->User页面用户具体信息User Group tab页
     def platform_user_member_information_user_group_tab(self):
          time.sleep(2)
          doc = "Platform User->User页面用户具体信息User Group tab页"
          self.move_mouse_to_click_element(AdminPUUserLocator.platform_user_member_info_user_group_tab, doc)
          time.sleep(1)
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_info_user_group_field, doc).text, 'User Group')
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_info_members_field, doc).text, 'Members')

     # Platform User->User页面用户具体信息Audit tab页
     def platform_user_member_information_audit_tab(self):
          time.sleep(2)
          doc = "Platform User->User页面用户具体信息Audit tab页"
          self.move_mouse_to_click_element(AdminPUUserLocator.platform_user_member_info_audit_tab, doc)
          time.sleep(3)
          self.wait_elevisble(AdminPUUserLocator.platform_user_member_info_audit_account_email_field, doc)
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_info_audit_account_email_field, doc).text, 'Account Email')
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_info_audit_event_type_field, doc).text, 'Event Type')
          self.check_attribute(self.get_element(AdminPUUserLocator.platform_user_member_info_audit_remarks_field, doc).text, 'Remarks')

     # 创建一个user
     def add_new_member(self, org_name,role_name):
          doc='admin端创建一个新的user'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          time.sleep(2)
          for i in range(3):
               try:
                    self.wait_elevisble(AdminPUUserLocator.invite_new_user,doc)
                    self.click_element(AdminPUUserLocator.invite_new_user,doc)
                    time.sleep(5)
                    break
               except:
                    self.driver.refresh()
                    time.sleep(5)
                    continue
          email = CommonTool.generate_email()
          time.sleep(5)
          self.wait_elevisble(AdminPUUserLocator.new_user_email,doc)
          self.send_keys(AdminPUUserLocator.new_user_email, email,doc)
          self.move_mouse_to_click_element(AdminPUUserLocator.user_role_choose_list,doc)
          eles=self.get_elements(AdminPUUserLocator.role_options,doc)
          eles[0].click()
          time.sleep(2)
          self.excute_js(User.create_role_js.replace('@a',role_name))
          time.sleep(2)
          #self.click_element(AdminPUUserLocator().role_option_choose(role_name))
          self.send_keys(AdminPUUserLocator.org_option, org_name,doc)
          time.sleep(2)
          self.wait_elevisble(AdminPUUserLocator().org_option_choose(org_name),doc)
          self.click_element(AdminPUUserLocator().org_option_choose(org_name),doc)
          self.click_element(AdminPUUserLocator.invite_new_user_ok_button,doc)
          time.sleep(2)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)
          return email

          # 查询user
     def query_user(self, email):
          doc='admin端查询user'
          time.sleep(5)
          self.wait_elevisble(AdminPUUserLocator.account_phone_query_input,doc)
          self.keyboard_event(AdminPUUserLocator.account_phone_query_input,doc)
          time.sleep(2)
          self.send_keys(AdminPUUserLocator.account_phone_query_input, email,doc)
          time.sleep(5)
          self.click_element(AdminPUUserLocator.account_phone_query_button,doc)
          self.wait_eleinvisble(AdminPUUserLocator.loading_wait,doc)
          time.sleep(3)


          # 删除user
     def delete_user(self, email):
          doc='admin端删除user'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          for i in range(3):
               try:
                    self.wait_elevisble(AdminPUUserLocator.action_button,doc)
                    break
               except:
                    self.refresh_page()
                    self.query_user(email)
                    continue
          try:
               self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          except StaleElementReferenceException:
               ele = self.get_element(AdminPUUserLocator.action_button,doc)
               ele.click()
          time.sleep(2)
          self.operations_action('Delete')
          self.wait_elevisble(AdminPUUserLocator.delete_org_ack,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator.delete_org_ack,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)
          # self.click_element(UserLocator.account_phone_query_button)
          time.sleep(2)


     def page_filtering_function_retrieval(self,email):
          doc='admin端筛条件查询'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          self.wait_elevisble(AdminPUUserLocator.status_filter,doc)
          time.sleep(2)
          self.move_mouse_to_click_element(AdminPUUserLocator.status_filter,doc)
          self.wait_elevisble(AdminPUUserLocator.filter_inactive,doc)
          self.click_element(AdminPUUserLocator.filter_inactive,doc)
          self.click_element(AdminPUUserLocator.filter_ok,doc)
          self.wait_elevisble(AdminPUUserLocator.status,doc)
          ele = self.get_elements(AdminPUUserLocator.status,doc)
          status = ele[0].text
          return status


     def operations_action(self,action):
          doc='admin端获取操作类型'
          eles=self.get_elements(AdminPUUserLocator.operations,doc)
          for ele in eles:
               if ele.text==action:
                    ele.click()

     # changepassword弹窗
     def change_password_by_email(self, email, cmdopt, get_database_source):
          doc = 'admin端通过邮箱修改密码'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          self.click_element(AdminPUUserLocator.reset_password_button,doc)
          time.sleep(2)
          self.wait_elevisble(AdminPUUserLocator.change_pwd_send,doc)
          self.click_element(AdminPUUserLocator.change_pwd_send,doc)
          time.sleep(2)
          change_pwd_verification_code = BusinessCommonTools().get_verification_code(cmdopt, email, get_database_source)
          time.sleep(2)
          self.send_keys(AdminPUUserLocator.change_password_verificationcode_input, change_pwd_verification_code,doc)
          time.sleep(2)
          self.send_keys(AdminPUUserLocator.new_pwd, Logindata.reset_pwd,doc)
          time.sleep(2)
          self.send_keys(AdminPUUserLocator.confirm_new_pwd, Logindata.reset_confirm_pwd,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator.reset_pwd_ok,doc)
          self.wait_elevisble(Loginlocator.login_username_ele,doc)

     def admin_reset_org_user_pwd(self, email):
          doc = '主账号重置组织下面成员的密码'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          time.sleep(5)
          self.wait_elevisble(AdminPUUserLocator.reset_pwd, doc)
          self.move_mouse_to_click_element(AdminPUUserLocator.reset_pwd, doc)
          self.wait_elevisble(AdminPUUserLocator.delete_org_ack, doc)
          self.click_element(AdminPUUserLocator.delete_org_ack, doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator.member_delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt, doc)

     def edit_user_org_info(self,email,cmdopt):
          doc='admin端修改用户的组织信息'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          self.wait_elevisble(AdminPUUserLocator.action_button,doc)
          self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          time.sleep(2)
          self.get_operation_name_click('Edit Organization')
          self.wait_elevisble(AdminPUUserLocator.edit_org_select,doc)
          self.click_element(AdminPUUserLocator.edit_org_select,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator().org_option_choose(Organization.default_organization[cmdopt]['admin']),doc)
          self.click_element(AdminPUUserLocator.edit_org_select,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     def get_operation_name_click(self, operation):
          doc = 'admin端通过操作的名称去点击操作'
          eles = self.get_elements(AdminPUUserLocator.operation,doc)
          for ele in eles:
               if ele.text == operation:
                    ele.click()

     def edit_user_group_info(self, email, usergroup):
          doc='admin端编辑用户的用户组信息'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          self.wait_elevisble(AdminPUUserLocator.action_button,doc)
          self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          time.sleep(2)
          self.get_operation_name_click('Edit User Group')
          self.wait_elevisble(AdminPUUserLocator.edit_user_group,doc)
          self.click_element(AdminPUUserLocator.edit_user_group,doc)
          time.sleep(2)
          self.click_element(AdminPUUserLocator().select_user_group(usergroup),doc)
          self.click_element(AdminPUUserLocator.edit_user_group,doc)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     #编辑用户信息
     def edit_user_role_info(self, email):
          doc='admin端编辑用户的角色信息'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          # self.wait_elevisble(OrganizationLocator.loading_wait)
          self.wait_elevisble(AdminPUUserLocator.action_button,doc)
          try:
               self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          except StaleElementReferenceException:
               ele = self.get_element(AdminPUUserLocator.action_button,doc)
               ele.click()
          time.sleep(2)
          self.get_operation_name_click('Edit Role')
          self.wait_elevisble(AdminPUUserLocator.edit_role,doc)
          self.click_element(AdminPUUserLocator.edit_role,doc)
          time.sleep(2)
          eles = self.get_elements(AdminPUUserLocator.role_options,doc)
          eles[0].click()
          time.sleep(2)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     def lock_user(self, email):
          doc='admin端锁定用户'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          try:
               self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          except StaleElementReferenceException:
               ele = self.get_element(AdminPUUserLocator.action_button,doc)
               ele.click()
          time.sleep(2)
          self.get_operation_name_click('Lock')
          time.sleep(2)
          self.wait_elevisble(AdminPUUserLocator.delete_org_ack,doc)
          self.click_element(AdminPUUserLocator.delete_org_ack,doc)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     def unlock_user(self, email):
          doc='admin端解锁用户'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          try:
               self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          except StaleElementReferenceException:
               ele = self.get_element(AdminPUUserLocator.action_button,doc)
               self.move_mouse_to_click_element(ele,doc)
          time.sleep(2)
          self.get_operation_name_click('Unlock')
          time.sleep(2)
          self.wait_elevisble(AdminPUUserLocator.delete_org_ack,doc)
          self.click_element(AdminPUUserLocator.delete_org_ack,doc)
          self.click_element(AdminPUUserLocator.delete_confirm,doc)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     def resend_user(self, email):
          doc='admin端重新激活用户'
          time.sleep(5)
          self.refresh_page()
          self.enter_the_user_page()
          self.query_user(email)
          self.wait_elevisble(AdminPUUserLocator.action_button,doc)
          time.sleep(2)
          self.move_mouse_to_click_element(AdminPUUserLocator.action_button,doc)
          time.sleep(2)
          self.get_operation_name_click('Resend')
          # time.sleep(2)
          # self.wait_elevisble(UserLocator.delete_acknowledge)
          # self.click_element(UserLocator.delete_acknowledge)
          # self.click_element(UserLocator.delete_confirm)
          self.wait_elevisble(AdminPUUserLocator.add_success_prompt,doc)

     # 验证列表字段 和 status列的值
     def list_columns(self):
          doc = "验证admin端User列表字段"

          self.enter_the_user_page()
          self.wait_eleinvisble(AdminPUUserLocator.loading_wait, doc)

          time.sleep(5)
          list_columns = self.get_elements(AdminPUUserLocator.list_columns, doc)
          assert list_columns[0].text == "Account Email"
          assert self.get_element(AdminPUUserLocator.list_column_status, doc).text == "Status"
          assert list_columns[2].text == "User Name"
          assert list_columns[3].text == "Phone Number"
          assert list_columns[4].text == "User Group"
          assert list_columns[5].text == "Organization"
          assert list_columns[6].text.startswith("Creation Time")
          assert list_columns[7].text == "Actions"

          self.wait_elevisble(AdminPUUserLocator.status_checkbox, doc)
          self.click_by_js(AdminPUUserLocator.status_checkbox, doc)
          self.wait_elevisble(AdminPUUserLocator.status_checkbox_options, doc)
          status_options = self.get_elements(AdminPUUserLocator.status_checkbox_options, doc)
          assert status_options[0].text == "Normal"
          assert status_options[1].text == "Locked"
          assert status_options[2].text == "Inactive"

          assert self.get_element(AdminPUUserLocator.reset_pwd_button, doc).text =="Reset Password"


          self.move_mouse_to_element(AdminPUUserLocator.user_options_button)

          assert self.get_element(AdminPUUserLocator.edit_organization_button, doc).text == "Edit Organization"
          assert self.get_element(AdminPUUserLocator.edit_user_group_button, doc).text == "Edit User Group"
          assert self.get_element(AdminPUUserLocator.edit_role_button, doc).text == "Edit Role"
          assert self.get_element(AdminPUUserLocator.lock_button, doc).text == "Lock"
          assert self.get_element(AdminPUUserLocator.unlock_button, doc).text == "Unlock"
          assert self.get_element(AdminPUUserLocator.delete_button, doc).text == "Delete"
          assert self.get_element(AdminPUUserLocator.resend_button, doc).text == "Resend"



     # 点击email跳转至用户具体信息界面
     def click_email(self):
         time.sleep(3)
         self.refresh_page()
         doc = "点击email进入用户具体信息界面"
         email_text = self.get_element(AdminPUUserLocator.email_button, doc).text
         self.click_by_js(AdminPUUserLocator.email_button, doc)

         time.sleep(3)
         self.wait_elevisble(AdminPUUserGroupLocator.user_info_page_title, doc)
         assert self.get_element(AdminPUUserGroupLocator.user_info_page_title, doc).text == email_text